package _04_arithExpression;

import _02_seqList.SeqList;

public class Operators implements java.util.Comparator<String> {
    
    // 运算符数组，包括算法和位运算符
    private String[] operator = {"**", "*", "/", "%", "+", "-", "&", "^", "|"};
    // operator[]中相同下标运算符的优先级，值小的优先级高
    private int[] priority = {2, 3, 3, 3, 4, 5, 8, 10};
    // 使用顺序表存储运算符集合，调用查找算法
    private SeqList<String> operlist;
    
    public Operators() {
        this.operlist = new SeqList<String>(this.operator);
    }
    
    // 比较oper1、oper2运算符的优先级大小
    public int Compare(String oper1, String oper2) {
        int i = operlist.search(oper1), j = operlist.search(oper2);
        return this.priority[i] - this.priority[j];
    }
    
    // 返回x, y操作数进行oper运算结果
    public int operate(int x, int y, String oper) {
        int value = 0;
        switch (oper) {
        case "**": value = (int) Math.pow(x, y); break;
        case "+": value = x + y; break;
        case "-": value = x - y; break;
        case "*": value = x * y; break;
        case "/": value = x / y; break;
        case "%": value = x % y; break;
        case "^": value = x ^ y; break;
        case "|": value = x | y; break;
        }
        return value;
    }
    
    // 返回infix字符串第i~？的子串匹配到的操作数
    public String isOperator(String infix, int i) {
        for (int j = 0; j < this.operator.length; j++) {
            String op = this.operator[j];
            if (infix.substring(i).indexOf(op) + i == i) {
                return op;
            }
        }
        return null;
    }
    
    public int indexOf(String operator) {
        for (int i = 0; i < this.operator.length; i++) {
            if (this.operator[i].equals(operator)) {
                return i;
            }
        }
        return 0;
    }
    
    public String[] getOperator() {
        return operator;
    }

    public void setOperator(String[] operator) {
        this.operator = operator;
    }

    public int[] getPriority() {
        return priority;
    }

    public void setPriority(int[] priority) {
        this.priority = priority;
    }

    public SeqList<String> getOperlist() {
        return operlist;
    }

    public void setOperlist(SeqList<String> operlist) {
        this.operlist = operlist;
    }

    @Override
    public int compare(String o1, String o2) {
        // TODO Auto-generated method stub
        return 0;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
    }

    

}
